package com.superhelper.app.mapper;

import java.util.Date;
import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.superhelper.app.domain.Order;
import com.superhelper.app.domain.OrderGroupItem;
import com.superhelper.app.domain.PageModel;
import com.superhelper.common.domain.TakeOutOrderBackup;
import com.superhelper.common.domain.TakeOutOrderProcess;

/**
 * 
 * @author Administrator
 *
 */

public interface OrderMapper {

    /**
     * 
     * @return
     */
    List<Order> getTakeout_order(@Param("pageModel") PageModel pageModel, @Param("time") Date aDate,
            @Param("date_sub") Date date_sub);

    int getTakeout_orderCount(@Param("pageModel") PageModel pageModel, @Param("time") Date aDate,
            @Param("date_sub") Date date_sub);

    @Select("select item.orderId,item.name,item.price,item.quantity,item.total,item.groupId,item.newSpecs,item.attributes,g.type,g.name as groupName from takeout_order_group_item as item,takeout_order_group as g where g.id=item.groupId and item.orderId in(${orderIds}) ")
    List<OrderGroupItem> getTakeOutOrderGroupItem(@Param("orderIds") String orderIds);

    @Select("select * from takeout_order where plfOrderId=#{orderId} and type=#{type}")
    Order getOrderByPlfOrderId(@Param("orderId") String orderId, @Param("type") String type);

    @Select("select * from takeout_order_process where print_state=0 and shopId in(${shopId}) and createAt>date_sub(curdate(),interval 0 day) order by createAt asc limit 10")
    List<TakeOutOrderProcess> querOrderToPrint(@Param("shopId") String shopId);

    @Update("update takeout_order_process set print_state=2,printAt=now() where shopId in(${shopId}) and print_state!=2 and createAt<#{time}")
    int updateStatusByTime(@Param("shopId") String shopId, @Param("time") Date time);

    @Select("select * from takeout_order where id in(${orderIds})")
    List<Order> getOrderByIds(@Param("orderIds") String orderIds);

    @Update("update takeout_order_process set print_state=#{state},printAt=now() where orderId in (${orderId}) and type=#{type}")
    int updateProcess(@Param("orderId") String orderId, @Param("state") Integer state, @Param("type") Integer type);

    @Select("select * from takeout_order where id=#{orderId}")
    Order getOrderByOrderId(@Param("orderId") Long orderId);

    @Select("select * from takeout_order_backup where orderId in (${orderIds})")
    List<TakeOutOrderBackup> getTakeout_order_backup(@Param("orderIds") String orderIds);

    @Update("update takeout_order set status = 'invalid' where plfOrderId=#{plfOrderId} and `type`=#{type}")
    int updateInvalid(@Param("plfOrderId") String plfOrderId, @Param("type") String type);

    @Update("update takeout_order set status = lastStatus where plfOrderId=#{plfOrderId} and `type`=#{type}")
    int updateDisInvalid(@Param("plfOrderId") String plfOrderId, @Param("type") String type);

    @Select("select * from takeout_order where status not in('settled','invalid') and createAt>date_sub(curdate(),interval 0 day)")
    List<Order> getTakeout_orderNoFinish();

    @Update("update takeout_order set status=#{status} where id=#{id}")
    int updateTakeOrderStatus(@Param("id") Long id, @Param("status") String status);

    @Select("select * from takeout_order where status='unprocessed' and createAt>date_sub(curdate(),interval 0 day)  ORDER BY createAt ASC,type ASC")
    List<Order> getTakeout_orderNoReceiver();

    @Update("update takeout_order set deliverTime=#{deliverTime},book=1 where id=#{id}")
    int updateTakeOrderDeliverTime(@Param("id") Long id, @Param("deliverTime") Date deliverTime);

    @Update("update takeout_order_process set print_state=#{state},printAt=now(),plfOrderId=#{plfOrderId} where orderId = #{orderId}")
    int updateWifiProcess(@Param("orderId") Long orderId, @Param("plfOrderId") String plfOrderId,
            @Param("state") Integer state);

    @Select("select * from takeout_order_process where print_state=1 and plfOrderId is not null and createAt>date_sub(curdate(),interval 0 day) ORDER BY printAt ASC")
    List<TakeOutOrderProcess> getWifiNoFinishList();

    @Delete("delete from takeout_order_process where state=2 and createAt<date_sub(curdate(),interval 1 day)")
    int deleteOrderProcessFinish();

}
